--[[
MIT License

Copyright (c) 2023 Anggara Yama Putra

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--]]
-- Globals
AnggaraNothing = AnggaraNothing or {}
do
end

-- class: SetArgField
do
  local root = {}

  local tabledefaults = AnggaraNothing.table_defaults
  local stringformat = string.format
  local MISSING = AnggaraNothing.sentinel('MISSING')

  function root:Create (name, props)
    props = tabledefaults(props, {
      default=MISSING,
      default_factory=MISSING,
    })
    local instance = tabledefaults({
      name = name,
      value = MISSING,
    }, props)
    if instance.default_factory ~= MISSING and instance.default ~= MISSING then
      error( 'cannot specify both default and default_factory', 1 )
    end

    local mt = root.m_metatable
    setmetatable(instance, tabledefaults(
      {
        __name = stringformat( '%s(%s)', mt.__name, name )
      }
    , mt))
    return instance
  end

  -- Events
  do
    function root:OnSet (oldvalue)
    end
  end

  local metatable = {}
  metatable.__index = root
  metatable.__name = 'SetArgField'
  metatable.__type = 'SetArgField'

  root.m_metatable = metatable
  root.MISSING = MISSING
  AnggaraNothing.SetArgField = root
end

-- class: SetArg
do
  local root = {}

  local ttype = AnggaraNothing.type
  local SetArgField = AnggaraNothing.SetArgField
  local MISSING = SetArgField.MISSING

  function root:Create (owner, fields)
    local instance = {
      m_owner = owner,
      m_fields = {},
    }

    local mt = root.m_metatable
    setmetatable( instance, mt )
    instance:_Init( fields )
    return instance
  end

  function root:Get ()
    local result = {}
    for k,v in pairs(self.m_fields) do
      result[k] = v.value
    end
    return result
  end

  function root:Set (args)
    for k,v in pairs(args) do
      local field = self.m_fields[k]
      if field then
        local old = nil
        old, field.value = field.value, v
        field:OnSet( old )
      end
    end
  end

  function root:Init ()
    -- One-time only.
    self.Init = nil
    return self:_Init( self.m_fields )
  end

  function root:_Init (fields)
    for k,v in pairs(fields) do
      local field = v
      if ttype(field) == 'SetArgField' then
        field.name = k
      else
        field = SetArgField:Create( k, {default=v} )
      end
      local field_val = field.value
      if field_val == MISSING then
        if field.default_factory ~= MISSING then
          field_val = field.default_factory()
        elseif field.default ~= MISSING then
          field_val = field.default
        end
      end
      field.value = field_val
      self.m_fields[k] = field
      self:Set( {[k]=field_val} )
    end
  end

  local metatable = {}
  function metatable:__index (key)
    local result = rawget( root, key )
    local fields = self.m_fields
    if fields then
      local field = fields[key]
      if field then
        result = field
      end
    end
    return result
  end
  metatable.__name = 'SetArg'
  metatable.__type = 'SetArg'

  root.m_metatable = metatable
  AnggaraNothing.SetArg = root
end
